import request from '@/utils/request'

/**
 * 获取轮播图列表
 * @param {Object} params - 查询参数，包括页码、每页数量、关键词、状态等
 * @returns {Promise} 返回轮播图列表数据
 */
export const getBannerList = (params) => {
  // 过滤掉空字符串、null和undefined值
  const filteredParams = {}
  if (params) {
    Object.keys(params).forEach(key => {
      if (params[key] !== undefined && params[key] !== null && params[key] !== '') {
        filteredParams[key] = params[key]
      }
    })
  }
  
  return request({
    url: '/admin/banners',
    method: 'get',
    params: filteredParams
  })
}

// 获取轮播图详情
export function getBannerDetail(id) {
  return request({
    url: `/admin/banners/${id}`,
    method: 'get'
  })
}

/**
 * 创建轮播图
 * @param {Object} data - 轮播图数据
 * @returns {Promise} 返回创建结果
 */
export const createBanner = (data) => {
  return request({
    url: '/admin/banners',
    method: 'post',
    data
  })
}

/**
 * 更新轮播图
 * @param {number} id - 轮播图ID
 * @param {Object} data - 更新数据
 * @returns {Promise} 返回更新结果
 */
export const updateBanner = (id, data) => {
  return request({
    url: `/admin/banners/${id}`,
    method: 'put',
    data
  })
}

/**
 * 删除轮播图
 * @param {number} id - 轮播图ID
 * @returns {Promise} 返回删除结果
 */
export const deleteBanner = (id) => {
  return request({
    url: `/admin/banners/${id}`,
    method: 'delete'
  })
}

// 更新轮播图状态
export const updateBannerStatus = (id, status) => {
  return request({
    url: `/admin/banners/${id}/status`,
    method: 'put',
    data: { status }
  })
}

// 更新轮播图排序
export const updateBannerSort = (id, sort) => {
  return request({
    url: `/admin/banners/${id}/sort`,
    method: 'put',
    data: { sort: parseInt(sort, 10) }
  })
}

// 批量删除轮播图
export const batchDeleteBanners = (ids) => {
  return request({
    url: '/admin/banners/batch',
    method: 'delete',
    data: { ids }
  })
}

// 上传轮播图
export const uploadBannerImage = (data) => {
  return request({
    url: '/admin/upload/banner',
    method: 'post',
    headers: {
      'Content-Type': 'multipart/form-data'
    },
    data
  })
} 